//
//  KLMRoomListCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/2/20.
//

import UIKit

protocol KLMRoomListCellDelegate: AnyObject{
    func roomListCellTapDelete(cell: KLMRoomListCell, index: IndexPath)
    func roomListCellLongPress(cell: KLMRoomListCell, index: IndexPath)
}

class KLMRoomListCell: UICollectionViewCell, Nibloadable {

    @IBOutlet weak var bgImgView: UIImageView!
    @IBOutlet weak var nameButton: UIButton!
    @IBOutlet weak var deleteBtn: UIButton!
    
    private var index: IndexPath = []
    weak var delegate: KLMRoomListCellDelegate?
    
    var cellCard : KLMCard! {
        didSet {
            if cellCard.subType == .add {
                nameButton.setImage(UIImage(named: cellCard.titleIcon ?? "add"), for: .normal)
                deleteBtn.isHidden = true
            } else {
                nameButton.setImage(nil, for: .normal)
                deleteBtn.isHidden = !cellCard.isDelete
            }
            
            nameButton.setTitle(cellCard.title, for: .normal)
        }
    }
    
    static func reusableCellCell(with collectionView: UICollectionView, reuseId: String, index: IndexPath) -> Self {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseId, for: index) as? Self {
            cell.index = index
            return cell
        }
        return Self.loadNib()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        nameButton.isUserInteractionEnabled = false
        nameButton.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 16), titleColor: rgba(38, 38, 38, 1))
        nameButton.titleLabel?.numberOfLines = 0
        
        layer.shadowColor = rgb(0, 0, 0).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 2)
        layer.shadowOpacity = 0.4
        layer.shadowRadius = 6
        layer.masksToBounds = false
        
        let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress))
        longPress.minimumPressDuration = 1
        self.addGestureRecognizer(longPress)
    }
    @objc func longPress(_ gesture: UILongPressGestureRecognizer) {
        
        switch gesture.state {
        case .began:
            if self.delegate != nil, cellCard.subType != .add {
                self.delegate?.roomListCellLongPress(cell: self, index: index)
            }
        default:
            break
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if cellCard.select {
            bgImgView.image = UIImage.image(withColor: appMainThemeColor, size: contentView.size, cornerRadius: 16)
        } else {
//            bgImgView.image = UIImage.image(withColor: rgba(247, 247, 247, 1), size: contentView.size, cornerRadius: 16, borderColor: rgb(187, 187, 187), borderWidth: 2)
            bgImgView.image = UIImage.image(withColor: rgba(247, 247, 247, 1), size: contentView.size, cornerRadius: 16)
        }
    }
    @IBAction func tapDelete(_ sender: UIButton) {
        if (self.delegate != nil) {
            self.delegate?.roomListCellTapDelete(cell: self, index: index)
        }
    }
    
}
